package com.thirteen.app.controller;


import com.thirteen.app.aop.SystemControllerLog;
import com.thirteen.app.entity.Course;
import com.thirteen.app.service.CourseService;
import com.thirteen.app.utils.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程(Course)表控制层
 *
 * @author makejava
 * @since 2021-09-03 14:44:41
 */
@RestController
@EnableOpenApi
@RequestMapping("/api/admin/education/subject")
@CrossOrigin
@Api("课程管理模块")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    @SystemControllerLog(description = "列出所有课程")
    @ApiOperation(value = "列出所有课程")
    @GetMapping("/list")
    public CommonResult listCourse() {
        return CommonResult.success(courseService.listAll());
    }

    @ApiOperation(value = "添加课程")
    @SystemControllerLog(description = "添加课程")
    @PostMapping("/addCourse")
    public CommonResult addCourse(@RequestBody Course course) {

        return CommonResult.success(courseService.insert(course));
    }

    @ApiOperation(value = "查询课程")
    @SystemControllerLog(description = "查询课程")
    @GetMapping("/select")
    public CommonResult select(Course course) {
        return CommonResult.success(courseService.queryById(course.getCourseId()));
    }

    @ApiOperation(value = "编辑课程")
    @SystemControllerLog(description = "编辑课程")
    @PostMapping("/edit")
    public CommonResult editCourse(@RequestBody Course course) {

        System.out.println(course);
        return CommonResult.success(courseService.update(course));

    }

    @ApiOperation(value = "删除课程")
    @SystemControllerLog(description = "删除课程")
    @PostMapping("/delete")
    public CommonResult deleteCourse(@RequestBody Course course) {
        return CommonResult.success(courseService.deleteById(course.getCourseId()));
    }

    @ApiOperation(value = "分页查询课程信息")
    @SystemControllerLog(description = "分页查询课程信息")
    @GetMapping("/page")
    public CommonResult listPageCourse(Integer pageIndex, Integer pageSize, String subject, String academy) {//课程名，学院ID
        Map<String, Object> map = new HashMap<>();
        map.put("data", courseService.queryAllByLimit(pageIndex, pageSize, subject, academy));
        map.put("total", courseService.getCourseCount(subject, academy));
        return CommonResult.success(map);
    }

}
